import {observable,action} from 'mobx';
import isEqual from 'lodash/isEqual';
import WeaUtils from '../components/Component/wea-utils'
const {GetRegex,DateFormat} = WeaUtils

import Apis from '../apis/myAttention';
import BlogApis from '../apis/myBlog';
import {message} from 'antd';
import * as attCon from '../constants/AttentionConstant';
import objectAssign from 'object-assign'

import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

var Base64 = ecCom.WeaTools.Base64

export default class MyAttention {
	@observable loading = false;
	@observable userList = [];
	@observable status = {
		title: getLabel(26469,"我的关注"),
		loading: false,
		countLoaded: false,//右侧各日期的关注人员情况是否加载完毕
		myAttLoaded: false,//左侧我的关注列表是否加载完毕
		myAttLasted: false,//左侧我的关注列表是否已到最末页
		myAttentions: [],// 我的关注列表
		rightType: attCon.RIGHT_TYPE_WEIBO,//右侧显示的类型，包括微博、用户。默认是微博
		myCountList: [],//按时间统计的微博数列表
		searchWeiboList: {},//搜索后的微博列表
		userId: '',//他的微博对应的人员id
		userInfo: {},//当前登录人员
		type:'',
		isOrg:false, //是不是组织结构页面
		morelading:false
	};
	constructor() {
			
	}
	
	changeMoreLoading = (val) => {
		this.status.morelading=val;
	}
	
	@action
	addUser(params) {
		this.userList.push({name:"dyf"});
	}
	getUserList(params) {
		return this.userList.slice();
	}
	doLoading = (loading) =>{
    this.status = {...this.status,loading:loading}
  }
	// export const doLoading = loading => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.MYATTENTION_LOADING,
	// 			loading
	// 		});
	// 	}
	// };


	setType = (type) => {
		this.status.type = type;
	}


	setOrg = (parms) => {
		this.status.isOrg = parms;
	}

	MYATTENTION_LIST_LOADING = (myAttLoaded) =>{
    this.status = {...this.status,myAttLoaded:myAttLoaded}
  }
	
	// export const getMyAttentions = (params) => {
	// 	return (dispatch, getState) => {
	// 			dispatch({
	// 						type: types.MYATTENTION_LIST_LOADING
	// 				});
	// 		Apis.getMyBlogAttentions(params)
	// 			.then(result => {
	// 				dispatch({
	// 					type: types.MYATTENTION_LIST,
	// 					data: result,
	// 				  params: params
	// 				})
	// 			})
	// 			.catch(error => message.error(error));
	// 	}
	// };

	getMyAttentions = (params)=>{
		this.MYATTENTION_LIST_LOADING(false);
		Apis.getMyBlogAttentions(params).then(result => {
			//this.clearLeft();
			let serverTotal = result.total || 0;
			let webTotal = (params.currentPage || 1) * (params.pageSize || 0);
			let myAttLasted = serverTotal <= webTotal;
			// let attentionList = [].concat(this.status.myAttentions).concat(result.attentionList);

			let attentionList = [];
			if(params.currentPage == 1){
				this.clearLeft();
				attentionList = [...result.attentionList];
			}else{
				attentionList = [...this.status.myAttentions,...result.attentionList];
			}
			//let attentionList = [...this.status.myAttentions,...result.attentionList];
			this.status =  {...this.status, myAttentions: attentionList, myAttLoaded: true, myAttLasted: myAttLasted};
		}).catch(error => {
			message.error(error)
		})
  	}
	
	// export const getMyAttentionShare = (params) => {
	// 		return (dispatch, getState) => {
	// 				dispatch({
	// 						type: types.MYATTENTION_LIST_LOADING
	// 				});
	// 				Apis.getMyAttentionShare(params)
	// 						.then(result => {
	// 								dispatch({
	// 										type: types.MYATTENTION_LIST,
	// 										data: result,
	// 										params: params
	// 								})
	// 						})
	// 						.catch(error => message.error(error));
	// 		}
	// };
	
	getMyAttentionShare = (params)=>{
		this.MYATTENTION_LIST_LOADING(false);
		Apis.getMyAttentionShare(params).then(result => {
			//this.clearLeft();
			let serverTotal = result.total || 0;
			let webTotal = (params.currentPage || 1) * (params.pageSize || 0);
			let myAttLasted = serverTotal <= webTotal;
			// let attentionList = [].concat(this.status.myAttentions).concat(result.attentionList);

			let attentionList = [];
			if(params.currentPage == 1){
				this.clearLeft();
				attentionList = [...result.attentionList];
			}else{
				attentionList = [...this.status.myAttentions,...result.attentionList];
			}
			//let attentionList = [...this.status.myAttentions,...result.attentionList];
			this.status =  {...this.status, myAttentions: attentionList, myAttLoaded: true, myAttLasted: myAttLasted};
		}).catch(error => {
			message.error(error)
		})
	}

	// export const editAttention = (params, callback) => {
	// 	return (dispatch, getState) => {
	// 		Apis.editAttention(params).then(
	// 			result => {
	// 					callback && callback(result.attnInfo);
	// 						}
	// 		).catch(
	// 			error => {
	// 				message.error(error)
	// 			}
	// 		)
	// 	}
	// };

	editAttention = (params, callback)=>{
    Apis.editAttention(params).then(result => {
        if (callback) callback(result.attnInfo)
    }).catch(error => {
        message.error(error)
    })
	}

	MYATTENTION_COUNTLIST_LOADING = (countLoaded) =>{
    this.status = {...this.status,countLoaded:countLoaded}
  }
	
	// export const getAttentionCountInfos = (params) => {
	// 	return (dispatch, getState) => {
	// 			dispatch({type: types.MYATTENTION_COUNTLIST_LOADING});
	// 		Apis.getAttentionCountInfos(params).then(
	// 			result => {
	// 				dispatch({
	// 					type: types.MYATTENTION_COUNTLIST,
	// 					data:result
	// 				})
	// 			}
	// 		).catch(
	// 			error => {
	// 				message.error(error)
	// 			}
	// 		)
	// 	};
	// };

	getAttentionCountInfos = (params)=>{
		this.MYATTENTION_COUNTLIST_LOADING(false);
    Apis.getAttentionCountInfos(params).then(result => {
				this.status =  {...this.status, myCountList: result.countInfos, weiboListArray: new Array(result.countInfos.length), countLoaded: true};
    }).catch(error => {
        message.error(error)
    })
  }
	
	
	// export const getAttentionWorkDateList = (params, index) => {
	// 	return (dispatch, getState) => {
	// 		Apis.getAttentionWorkDateList(params).then(
	// 			result => {
	// 				dispatch({
	// 					type: types.MYATTENTIONWORK_BY_DATE,
	// 					data: result,
	// 					index: index
	// 				})
	// 			}
	// 		).catch(
	// 			error => {
	// 				message.error(error)
	// 			}
	// 		)
	// 	};
	// };




    isBase64 = (str) => {
        if(str.indexOf("PHA+")>-1){
            return true;
        }else {
            return false;
        }
    }
//加载下一页
    addMore = (prams,index) =>{
		  this.changeMoreLoading(true);
        let dd =this.status.myCountList[index];
        prams.currentPage=dd.currentPage+1;
        prams.pageSize=dd.pageSize;
        this.getAttentionWorkDateList(prams,index);
    }


    getAttentionWorkDateList = (params, index)=>{
        Apis.getAttentionWorkDateList(params).then(result => {
            if (!this.status.myCountList || !this.status.myCountList[index]){
                // return
                this.status = {...this.status}
            }else{
              
                let copyresult = { ...result }
                copyresult.discussList.forEach((record, index) => {
                    if (record.content) {
                        let decodeContent =record.content;
                      
                            decodeContent = Base64.decode(record.content)
                        
                        copyresult.discussList[index] = { ...record, content: decodeContent }
                    }
                    if (record.blogReplyList && record.blogReplyList.length > 0) {
                        record.blogReplyList.forEach((_record, _index) => {
                            let decodeContentReply =_record.content;
                           
                                decodeContentReply = Base64.decode(_record.content)
                            
                            copyresult.discussList[index].blogReplyList[_index] = { ..._record, content: decodeContentReply }
                        })
                    }
                })
				
				let data = copyresult;

                data.__hasData = data.discussList ? !!data.discussList.length : null

                if (data.currentPage == 1) {
                    data.discussList = data.discussList
                } else {
                    // data.discussList = this.status.searchWeiboList.discussList.concat(data.discussList || [])
                    let others = data.discussList || [];
                    data.discussList = [...this.status.myCountList[index].data.discussList,...others];
                }
				
                let hasMore = 0;
                hasMore = data.hasMore;
                let cdata = {...this.status.myCountList[index], data:copyresult, loaded: true,currentPage:params.currentPage,pageSize:params.pageSize,hasMore:hasMore};
                let newstate = {...this.status};
                // newstate.myCountList = objectAssign([], this.status.myCountList);
                newstate.myCountList[index] = cdata;
                this.status =  {...newstate};
                this.changeMoreLoading(false);
            }
        }).catch(error => {
            message.error(error)
        })
    }


    // export const chooseUser = (userId) => {
	// 	return {
	// 		type: types.MYATTENTION_CHOOSEUSER,
	// 		userId: userId
	// 	};
	// };

	chooseUser = (userId) =>{
		let attlist = this.status.myAttentions.map(att=>{
			if (att.userId == userId){
				return {...att, isnew:'0'};
			}
				return att;
			});
		this.status =  {...this.status, myAttentions:attlist, userId: userId, rightType: attCon.RIGHT_TYPE_USER};
  }


	// export const getCurrentUser = () => {
	// 	return (dispatch, getState) => {
	// 				BlogApis.getUserInfo().then(
	// 						result => {
	// 								dispatch({
	// 										type: types.CURRENT_USER,
	// 										data: result
	// 								})
	// 						}
	// 				).catch(
	// 						error => {
	// 								message.error(error)
	// 						}
	// 				)
	// 		};
	// };
	getCurrentUser = () =>{
		BlogApis.getUserInfo().then(result => {
			// this.status =  {...this.status, myCountList: result.countInfos, weiboListArray: new Array(result.countInfos.length), countLoaded: true};
			this.status =  {...this.status, userInfo:result.currentUser};
		}).catch(error => {
				message.error(error)
		})
  }

	// export const searchWeiboList = (params) => {
	// 		return (dispatch, getState) => {
	// 				dispatch({type: types.MYATTENTION_COUNTLIST_LOADING});
	// 				BlogApis.searchWeiboMainPageList(params).then(
	// 						result => {
	// 								dispatch({
	// 										type: types.MYATTENTION_SEARCHWEIBO,
	// 										data: result
	// 								})
	// 						}
	// 				).catch(
	// 						error => {
	// 								message.error(error)
	// 						}
	// 				)
	// 		};
	// };

	searchWeiboList = (params)=>{
		this.MYATTENTION_COUNTLIST_LOADING(false);
    BlogApis.searchWeiboMainPageList(params).then(result => {
				let data = result;

				data.__hasData = data.discussList ? !!data.discussList.length : null

				if (data.currentPage == 1) {
						data.discussList = data.discussList
				} else {
						// data.discussList = this.status.searchWeiboList.discussList.concat(data.discussList || [])
						let others = data.discussList || []
						data.discussList = [...this.status.searchWeiboList.discussList,...others]
				}
				let copyresult = { ...data }
				copyresult.discussList.forEach((record, index) => {
						if (record.content) {
								let decodeContent = Base64.decode(record.content)
								data.discussList[index] = { ...record, content: decodeContent }
						}
						if (record.blogReplyList && record.blogReplyList.length > 0) {
								record.blogReplyList.forEach((_record, _index) => {
										let decodeContentReply = Base64.decode(_record.content)
										data.discussList[index].blogReplyList[_index] = { ..._record, content: decodeContentReply }
								})
						}

				})
				this.status =  {...this.status, searchWeiboList: data, countLoaded: true}
    }).catch(error => {
        message.error(error)
    })
	}
	
	
	// export const sendRemindUnsubmitAll = (params) => {
	// 		return (dispatch, getState) => {
	// 				Apis.sendRemindUnsubmitAll(params).then(
	// 						result => {
	// 								message.success("已全部提醒！");
	// 						}
	// 				).catch(
	// 						error => {
	// 								message.error(error)
	// 						}
	// 				)
	// 		};
	// };

	sendRemindUnsubmitAll = (params) =>{
		Apis.sendRemindUnsubmitAll(params).then(result => {
			// this.status =  {...this.status, myCountList: result.countInfos, weiboListArray: new Array(result.countInfos.length), countLoaded: true};
			// this.status =  {...this.status, userInfo:result.currentUser};
			message.success(getLabel(385491,'已全部提醒！'));
			
		}).catch(error => {
				message.error(error)
		})
  }
	
	// export const updateUserAttentionStatus = attnInfo => {
	// 		return {
	// 				type: types.MYATTENTION_UPDATE_USER_ATTENTIONSTATUS,
	// 				attnInfo: attnInfo
	// 		};
	// };

	updateUserAttentionStatus = (attnInfo) =>{
		let {userId,attentionStatus} = attnInfo;
		let attlist = this.status.myAttentions.map(att=>{
				if (att.userId == userId)
						return {...att, attentionStatus: attentionStatus};
				return att;
		});
		this.status = {...this.status, myAttentions:attlist};
  }
	
	// export const resetRight = () => {
	// 	return {type: types.MYATTENTION_RESETRIGHT};
	// };
	resetRight = () =>{
		this.status =  {...this.status, userId:'', rightType: attCon.RIGHT_TYPE_WEIBO};
  }
	
	// export const clearLeft = () => {
	// 		return {type: types.MYATTENTION_CLEARLEFT};
	// };
	clearLeft = () =>{
		this.status =  {...this.status, myAttentions:[]};
  }
	// export const clearRightList = () => {
	// 	return {type: types.MYATTENTION_CLEARRIGHTLIST};
	// };
	clearRightList = () =>{
		this.status =  {...this.status, searchWeiboList:{}, countLoaded: false};
  }


}